package cn.airfei.aircore.testcore.services.domainService.impl;

import cn.airfei.aircore.core.properties.AirCoreProperty;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.airfei.aircore.core.utils.QueryParamUtil;
import cn.airfei.aircore.testcore.storages.entity.User;
import cn.airfei.aircore.testcore.infrastructure.mapper.UserMapper;
import cn.airfei.aircore.testcore.services.domainService.UserDomainService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.airfei.aircore.testcore.storages.dto.UserDto;
import cn.airfei.aircore.testcore.storages.vo.UserVo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.math.NumberUtils;
import cn.airfei.aircore.core.constants.AirStatusConstants;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import cn.airfei.aircore.core.storage.PageParam;

/**
 * <p>
 * 用户表
 * </p>
 *
 * @author air
 * @since 2022-04-25
 */
@Service
public class UserDomainServiceImpl extends ServiceImpl<UserMapper, User> implements UserDomainService {
    @Resource
    private AirCoreProperty airCoreProperty;

    @Resource
    private UserMapper userMapper;


    /**
     *   
     * @param userDto
     * @param pageParam
     * @return
     */
    @Override
    public Page<UserVo> index(UserDto userDto,PageParam pageParam) {
        Page<UserDto> page=new Page<>(pageParam.getPageCurrent(),pageParam.getPageSize());
        return userMapper.index(page,userDto,pageParam);
    }

    /**
     * 分页查询单表数据
     *
     * @param map
     * @return
     */
    @Override
    public Page<User> singleTablePage(Map<String, Object> map) {
        QueryWrapper<User> queryWrapper= QueryParamUtil.getEntityWrapper(map,User.class);
        queryWrapper.lambda().orderByDesc(User::getId);
        Page<User> page=new Page<>(Long.parseLong(String.valueOf(map.get("pageCurrent"))),Long.parseLong(String.valueOf(map.get("pageSize"))));
        return userMapper.selectPage(page,queryWrapper);
    }

    /**
     * 显示单条数据
     *
     * @param id
     * @return
     */
    @Override
    public User show(String id){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return userMapper.selectOne(queryWrapper);
    }

    /**
     * 删除（销毁表数据）,逻辑删除,设置status为-1
     *
     * @param ids
     * @return
     */
    @Override
    public Integer destroy(String ids){
        List<Long> idLongList = Arrays.asList(ids.split(",")).stream()
            .map(s -> NumberUtils.createLong(s.trim())).collect(Collectors.toList());

        QueryWrapper<User> queryWrapper=new QueryWrapper();
        queryWrapper.in("id",idLongList);
        User user=new User();
        user.setStatus(AirStatusConstants.delete);
        return userMapper.update(user,queryWrapper);
    }

    /**
     * 更新表数据
     *
     * @param user
     * @return
     */
    @Override
    public Integer update(User user){
        return userMapper.updateById(user);
    }

    /**
     * 保存表数据
     *
     * @param user
     * @return
     */
    @Override
    public Integer store(User user){
        return userMapper.insert(user);
    }

    /**
     * 设置状态，1:正常,2:禁用/待审核，-1:已删除
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public Integer setStatus(Long id, Integer status) {
        User user=new User();
        user.setId(id);
        user.setStatus(status);
        return userMapper.updateById(user);
    }

    /**
     * get all resource in storage
     * @return
     */
    @Override
    public List<User> getAllUser() {
        QueryWrapper<User> queryWrapper=new QueryWrapper();
        queryWrapper.lambda()
            .orderByDesc(User::getId)
            .last("limit " + airCoreProperty.getMaxSelect());
        return userMapper.selectList(queryWrapper);
    }

    /**
     * get list in single table
     *
     * @param map
     * @return
     */
    @Override
    public List<User> getSingleTableList(Map map) {
        QueryWrapper<User> queryWrapper = QueryParamUtil.getEntityWrapper(map, User.class);
        queryWrapper.lambda().orderByDesc(User::getId).last("limit " + airCoreProperty.getMaxSelect());
        return userMapper.selectList(queryWrapper);
    }

}
